# s.bing(),s.listen(),s.accept(),s.connect(),s.recv(),s.send(),s.sendall(),
# s.sendto(),s.close()
import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('localhost', 8888))
server.listen(5)

client, addr = server.accept()
print('客户端连接成功', addr)
data = client.recv(1024)
print('收到数据', data.decode())
msg = input("请输入你要和客户端回复的消息：")

client.send(msg.encode())

client.close()
server.close()


def scan_port(ip, port):
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(1)
        result = sock.connect_ex((ip, port))
        if result == 0:
            print(f"Port{port}: Open")
        else:
            print(f"Port{port}: Close")
        sock.close()
    except Exception as e:
        print(f"Error scanning port{port}: {e}")


target_ip = "192.168.1.1"
start_port = 1
end_port = 1024
for port in range(start_port, end_port + 1):
    scan_port(target_ip, port)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 8888))
client.send('hello'.encode())

data = client.recv(1024)
print("客户端收到数据：", data.decode())

client.close()
